layui.define(["table", "form", "laydate", "element"], function (exports) {
  var $ = layui.$,
    table = layui.table,
    laydate = layui.laydate,
    element = layui.element;
  form = layui.form;

  //会议开始时间会议结束时间
  var start = laydate.render({
    elem: ".startDate",
    type: "datetime",
    trigger: "click", //采用click弹出
    done: function (value, date) {
      form.render();
      endMax = end.config.max;
      end.config.min = date;
      end.config.min.month = date.month - 1;
    },
  });
  var end = laydate.render({
    elem: ".endDate",
    type: "datetime",
    trigger: "click", //采用click弹出
    done: function (value, date) {
      form.render();
      if ($.trim(value) == "") {
        var curDate = new Date();
        date = {
          date: curDate.getDate(),
          month: curDate.getMonth() + 1,
          year: curDate.getFullYear(),
        };
      }
      start.config.max = date;
      start.config.max.month = date.month - 1;
    },
  });

  $("body").on("click", function () {
    // xxxxx
    laydate.render("#startDate");
    laydate.render("#endDate");
  });

  // $("body").on("click", "#startDate", function () {
  //   // xxxxx
  //   console.log("#startDate");
  //   layui.use(["laydate"], function () {
  //     //会议开始时间会议结束时间
  //     var laydate1 = layui.laydate;
  //     var start = laydate1.render({
  //       elem: "#startDate",
  //       type: "datetime",
  //       trigger: "click", //采用click弹出
  //       done: function (value, date) {},
  //     });
  //   });
  // });
  // $("body").on("click", "#endDate", function () {
  //   // xxxxx
  //   layui.use(["laydate"], function () {
  //     var laydate2 = layui.laydate;
  //     var end = laydate2.render({
  //       elem: "#endDate",
  //       type: "datetime",
  //       trigger: "click", //采用click弹出
  //       done: function (value, date) {},
  //     });
  //   });
  // });

  // tableSearch
  form.on("submit(tableSearch)", function (data) {
    table.reload("attendMeeting", { where: data.field });
    table.reload("departmentMeeting", { where: data.field });
    // table.reload("attendMeeting", { where: Object.assign({}, data.field) });
    // $('input[name="startTime"]').val(data.field.startTime);
    // $('input[name="endTime"]').val(data.field.endTime);
    // $('input[name="meet，ingName"]').val(data.field.meetingName);
    // $('input[name="meetingId"]').val(data.field.meetingId);
  });
  // tableRefresh
  form.on("submit(tableRefresh)", function (data) {
    table.reload("attendMeeting", {
      where: { startTime: "", endTime: "", meetingName: "" },
    });
    table.reload("departmentMeeting", {
      where: { startTime: "", endTime: "", meetingName: "" },
    });
  });

  //初始化变量
  let meetingType = 2,
    tableName = "#attendMeeting",
    mettingDone = "";
  getMeetingTable("#attendMeeting", "2", "");
  element.on("tab(meetingsManagementTab)", function (data) {
    if ($(this).attr("data-meetingType")) {
      meetingType = $(this).attr("data-meetingType");
    } else {
      mettingDone = $(this).attr("data-mettingDone");
    }
    if (meetingType == "2") {
      tableName = "#attendMeeting";
    } else if (meetingType == "1") {
      tableName = "#departmentMeeting";
    }

    console.log("tableName---------------------", tableName);

    getMeetingTable(tableName, meetingType, mettingDone);
  });

  // element.on("tab(test)", function (elem) {
  //   console.log("table----------------------------", table);
  //   location.hash = "test=" + $(this).attr("lay-id");

  //   table.render({
  //     elem: "#attendMeeting12",
  //     height: 312,
  //     url: "/demo/table/user/", //数据接口
  //     page: true, //开启分页
  //     cols: [
  //       [
  //         //表头
  //         { field: "id", title: "ID", width: 80, sort: true, fixed: "left" },
  //         { field: "username", title: "用户名", width: 80 },
  //         { field: "sex", title: "性别", width: 80, sort: true },
  //         { field: "city", title: "城市", width: 80 },
  //         { field: "sign", title: "签名", width: 177 },
  //         { field: "experience", title: "积分", width: 80, sort: true },
  //         { field: "score", title: "评分", width: 80, sort: true },
  //         { field: "classify", title: "职业", width: 80 },
  //         { field: "wealth", title: "财富", width: 135, sort: true },
  //       ],
  //     ],
  //   });
  //   table.reload();
  // });

  //会议管理

  function getMeetingTable(tableName, meetingType, mettingDone) {
    console.log(tableName, meetingType, mettingDone);
    var dom = tableName + meetingType + mettingDone;
    console.log(dom);

    // table.render({
    //   elem: dom,
    //   height: 312,
    //   url: "/demo/table/user/", //数据接口
    //   page: true, //开启分页
    //   cols: [
    //     [
    //       //表头
    //       { field: "id", title: "ID", width: 80, sort: true, fixed: "left" },
    //       { field: "username", title: "用户名", width: 80 },
    //       { field: "sex", title: "性别", width: 80, sort: true },
    //       { field: "city", title: "城市", width: 80 },
    //       { field: "sign", title: "签名", width: 177 },
    //       { field: "experience", title: "积分", width: 80, sort: true },
    //       { field: "score", title: "评分", width: 80, sort: true },
    //       { field: "classify", title: "职业", width: 80 },
    //       { field: "wealth", title: "财富", width: 135, sort: true },
    //     ],
    //   ],
    // });

    table.render({
      elem: dom,
      skin: "nob", // 无边框风格
      toolbar: "#toolbarOpear", //开启工具栏
      defaultToolbar: [],
      totalRow: true, //开启合计行
      url: base_url + "/meeting/listMeetings", //模拟接口
      headers: {
        Accept: "application/json; charset=utf-8",
        Authorization: layui.sessionData("userToken").key,
      },
      method: "post",
      contentType: "application/json",
      request: {
        pageName: "pageIndex", //页码的参数名称，默认：page
        limitName: "pageSize", //每页数据量的参数名，默认：limit
      },
      parseData: function (res) {
        return {
          code: 0,
          count: res.data.total,
          data: res.data.data,
          msg: "",
        };
      },
      cols: [
        [
          // {
          //   type: "checkbox",
          //   title: "选择",
          //   fixed: "left",
          //   width: "3%",
          //   fixed: "left",
          //   unresize: true,
          // },

          // {type: 'checkbox'}  这里我们不在使用table 本身的使用checkbox的方式来控制它
          {
            //这里使用layui给我们提供的自定义模版功能来达到checkbox控制效果
            title: "选择",
            width: 100,
            templet: function (d) {
              //记录当前数据渲染的是第几行
              var dIndex = d.LAY_TABLE_INDEX;
              //给每一个checkbox加一个id
              var id = "ck" + dIndex;
              var templateHtml =
                ' <input type="checkbox" id="' +
                id +
                '" class="layui-input layui-form-checkbox" lay-skin="primary"' +
                'lay-filter="ldEvent"><i class="layui-icon"></i>';
              //利用三元表达式判断本条数据是否需要加载checkbox
              return d.flag ? "" : templateHtml;
            },
          },
          { type: "numbers", title: "序号" },
          {
            field: "projectName",
            title: "所属项目",
            width: "10%",
            align: "center",
            unresize: true,
          },
          {
            field: "meetingName",
            title: "会议名称",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            field: "meetingOriginator",
            title: "会议发起人",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            field: "meetingUnits",
            title: "参会单位",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            field: "roomId",
            title: "会议ID",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            field: "startTime",
            title: "开始时间",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            field: "endTime",
            title: "结束时间",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            field: "",
            title: "会议总时长",
            align: "center",
            width: "10%",
            unresize: true,
          },
          {
            title: "操作",
            align: "center",
            fixed: "right",
            with: 170,
            unresize: true,
            toolbar: "#table-content-list",
          },
        ],
      ],
      page: true,
      limit: 6,
      limits: [5, 10, 15, 20, 25, 30],
      text: {
        none: "暂无数据",
      },
      where: {
        mettingDone,
        refresh: 2,
        meetingType,
      },
      done: function (res, curr, count) {
        // 表格渲染完成之后的回调
        var that = this.elem.next();
        res.data.forEach(function (item, index) {
          if (index % 2 == 0) {
            var tr = that
              .find(".layui-table-box tbody tr[data-index='" + index + "']")
              .css("background-color", "transparent");
          } else {
            var tr = that
              .find(".layui-table-box tbody tr[data-index='" + index + "']")
              .css("background", "#EDF2FB");
          }
        });
        //搜索按钮  附加监听
        addSearchListener(table);
      },
    });
  }
  function addSearchListener(table) {
    // tableSearch
    form.on("submit(tableSearch)", function (data) {
      console.log("tableSearch", data, $('input[name="meetingName"]').val());
      table.reload("attendMeeting", { where: Object.assign({}, data.field) });
      $('input[name="startTime"]').val(data.field.startTime);
      $('input[name="endTime"]').val(data.field.endTime);
      $('input[name="meetingName"]').val(data.field.meetingName);
      $('input[name="meetingId"]').val(data.field.meetingId);
    });
  }
  table.on("toolbar(attendMeeting)", function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    switch (obj.event) {
      case "layTableAllChoose":
        layer.msg("这是全选", { icon: 6, anim: 6 });
        break;
    }
  });

  exports("meetingsManagement", {});
});
